﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data .SqlClient ;

using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Security.Cryptography;
using System.Windows.Forms;


namespace community
{
    class dataBase
    {   //用md5加密
        public static string encode(string data)
        {
            string pwd = "";
            MD5 md5 = MD5.Create();
            byte[] s = md5.ComputeHash(Encoding.Unicode.GetBytes(data));//用MD5算法加密
            for (int i = 0; i < s.Length; i++)
            {
                // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母，如果使用大写（X）则格式后的字符是大写字符
                pwd = s[i].ToString("X");
            }
            return pwd;

        }

    }

    class SQL
    {
        //林兰芬
        //public static string conStr = "Data Source=222.16.82.144;Initial Catalog=community;Persist Security Info=True;User ID=sa;Password=sql";

        //李晓杰
        //public static string conStr = @"Data Source=222.16.73.222\sqlexpress;Initial Catalog=community;Persist Security Info=True;User ID=sa;Password=sql";

        //本机
        //public static string conStr = @"Data Source=localhost;Initial Catalog=community;trusted_connection=yes";
        //华辉
        public static string conStr = @"Data Source=222.16.73.220;Initial Catalog=community;Persist Security Info=True;User ID=sa;Password=sql";

    
    }

    class sql_database
    {                   
        
        string strConn = SQL.conStr;

        SqlConnection conn;
        
        SqlDataAdapter da; 

        //把数据库的内容呈现到datagridview中。
        public   void show_dataGridview(string sql, string tableName, DataGridView dg)
        {
            try
            {
                using (conn = new SqlConnection(strConn))
                {
                    da = new SqlDataAdapter(sql, conn);

                    DataSet ds = new DataSet();
                    if (conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }
                    int count = da.Fill(ds, tableName);

                    dg.DataSource = ds.Tables[tableName];
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        //insert into 到数据库
        public  void new_row(string sqlText)
        {


            using (conn = new SqlConnection(strConn))
            {
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                SqlCommand com = new SqlCommand(sqlText, conn);

                int i;
                try
                {
                    i = com.ExecuteNonQuery();
                    if (i == 1)
                        MessageBox.Show("恭喜！保存成功！");
                    else
                    {

                        MessageBox.Show("保存失败！");
                    }
                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.ToString());
                }


            }

        }

        //获取表的记录数+1
        public  int getSerial(string tableName ,string columnName )
        {
            int max_number =0;

            using (SqlConnection con = new SqlConnection(strConn))
            {
                if (con.State == ConnectionState.Closed)
                    con.Open();
                string str2 = "select max( "+columnName + " )  from " + tableName;//获取最大序号

                //获取职工号
                SqlCommand  com = new SqlCommand(str2, con);
                SqlDataReader rdr = com.ExecuteReader();
                while (rdr.Read())
                {
                    max_number = Convert.ToInt32(rdr[0]) ;
                    //MessageBox.Show(max_number.ToString());
                }
                rdr.Close();
            }

            return (max_number + 1);

        }


    }

}
